package com.lhl.jwt.config;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.jackson.JsonComponent;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @className: DateFormatConfig
 * @description: TODO 类描述
 * @author: king
 * @date: 2020-09-07 16:36
 **/
@JsonComponent
@Slf4j
public class DateFormatConfig {
    public static class DateTimeSerializer extends JsonSerializer<LocalDateTime> {

        private DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        @Override
        public void serialize(LocalDateTime localDateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeString(dateTimeFormatter.format(localDateTime));
        }
    }


    public static class DateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
        @Override
        public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
            if (StrUtil.isNotBlank(jsonParser.getText())) {
                return LocalDateTime.parse(jsonParser.getText(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            }
            return null;
        }
    }

    /**
     * @param: null
     * @description: 把数据库获得日期进行序列化,按照既定格式
     * @return:
     * @author: king
     * @date: 2020-09-07 17:50
     */
    public static class DateSerializer extends JsonSerializer<LocalDate> {

        private DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        @Override
        public void serialize(LocalDate localDate, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeString(dateFormatter.format(localDate));
        }
    }
    /**
     * @param: LocalDate
     * @description: 反序列化,把传过来的日期进行解码
     * @return:
     * @author: king
     * @date: 2020-09-07 17:48
     */
    public static class DateDeserializer extends JsonDeserializer<LocalDate> {
        @Override
        public LocalDate deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
            log.info("-----------------{},{}", jsonParser.getText(), jsonParser.getValueAsString());
            if (StrUtil.isNotBlank(jsonParser.getText())) {
                return LocalDate.parse(jsonParser.getText(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            }
            return null;
        }
    }
}